<?php

    class videomodule {
        function name() { return 'Video Module'; }
        function author() { return 'Trung Dang - ABSoft Viet Nam - http://absoft.vn'; }
        function description() { return 'Show videos'; }

        function hasContent() { return true; }
        function hasSources() { return true; }
        function hasViews()   { return true; }

        function supportsWorkflow() { return false; }

        function permissions($internal = '') {

            return array(
            'administrate'=>'Administrate',
            'configure'=>'Configure',
            'create'=>'Create',
            'delete'=>'Delete',
            'edit'=>'Edit',
            );
        }

        function getLocationHierarchy($loc) {
            if ($loc->int == '') {
                return array($loc);
            } else {
                return array($loc,exponent_core_makeLocation($loc->mod,$loc->src));
            }
        }

        function show($view,$loc = null, $title = '') {
            global $user;
            global $db;
            
            $sql_where = "";
            $temp=serialize($loc);
            
            $cats=$db->selectObjects("videomodule_cat", "location_data = '{$temp}'");
            
            $new_video=$db->selectObjects("videomodule_video","location_data = '{$temp}' {$sql_where} ORDER BY postdate DESC LIMIT 0, 3");
            $new_video=videomodule::__VideoProcess($new_video);

            $highlighted_video=$db->selectObjects("videomodule_video","location_data = '{$temp}' {$sql_where} ORDER BY views DESC LIMIT 0, 3");
            $highlighted_video=videomodule::__VideoProcess($highlighted_video);

            // fetch to template
            $template=new template("videomodule",$view,$loc);
            $template->register_permissions(array('administrate',"configure", "create", "edit", "delete"),$loc);
            $template->assign('loc', $loc);
            $template->assign("cats", $cats);
            // for playlist in bottom
            $template->assign("new_video", $new_video);
            $template->assign("new_video_count", count($new_video));

            $template->assign("highlighted_video", $highlighted_video);
            $template->assign("highlighted_video_count", count($highlighted_video));
            
            $template->output();

        }

        function spiderContent($item = null) {
            // No searchable content
            return false;
        }

        function deleteIn($loc) {

        }

        function copyContent($oloc,$nloc) {

        }

        static function __VideoProcess($videos, $load_video=0){
            global $db;

            if (!is_array($videos))
                $videos=array($videos);

            $cache_cat=array();

            foreach ($videos as $k=>$v)
            {
                $v->title_in_lower=strtolower($v->title);

                if (isset($cache_cat[$v->cat_id]))
                {
                    $v->cat_title=$cache_cat[$v->cat_id]->title;       
                    $v->cat_seo=$cache_cat[$v->cat_id]->seo_name;       
                }
                else
                {
                    $cat=$db->selectObject("videomodule_cat", "id = {$v->cat_id}");
                    $v->cat_title=$cat->title;
                    $v->cat_seo=$cat->seo_name;
                    $cache_cat[$cat->id]=$cat;
                }

                if ($load_video == 1)
                {
                    if ($v->video_type == 2)
                    {
                        $v->file_path="http://www.youtube.com/watch?v=".$v->youtube_embed;
                    }
                    else
                    {
                        $file=$db->selectObject("file", "id = {$v->file_id}");
                        if ($file)
                            $v->file_path=URL_FULL.$file->directory."/".$file->filename;
                        else
                            $v->file_path="";
                    }
                }
                if (($v->video_type == 2 && $v->use_own_thumbnail == 1) || ($v->video_type == 0)){
                    //
                    $file=$db->selectObject("file", "id = {$v->thumbnail}");
                    if ($file)
                        $v->thumbnail=URL_FULL.$file->directory."/".$file->filename;
                    else
                        $v->thumbnail="";
                }
                else if ($v->video_type == 2){
                        // generate youtube thumbnail
                        $v->thumbnail="http://img.youtube.com/vi/{$v->youtube_embed}/0.jpg";
                    }
                    else if ($v->video_type == 1)
                            $v->thumbnail=URL_FULL.THEME_RELATIVE."modules/videomodule/images/mp3.png";
                        //


                        // url backlink
                        $v->url_link=exponent_core_makeLink(array(
                "module"=>"videomodule",
                "action"=>"_video_main",
                "src"=>unserialize($v->location_data)->src,
                "cat_name"=>$v->cat_seo,
                "video_name"=>$v->seo_name
                ))."#".$v->seo_name;
            }
            return $videos;
        }
    }

?>